


  #include <opencv2/core/core.hpp>
 #include <opencv2/highgui/highgui.hpp>
 #include <opencv2/imgproc/imgproc.hpp>
 #include <iostream>

 using namespace std;
 using namespace cv;

 int main(int argc ,char ** argv)
 {
	 bool g_step=false ;
	 if(argc<3){
		 cout<<"usage ./cv_utils_imgdiff -i vieofile [--step]"<<endl;
		 return -1 ;
	 }
     VideoCapture capture(argv[2]);
     if(argc==4){
    	 string step(argv[3]);
    	 if(step=="--step"){
    		 g_step = true ;
    	 }
     }
     if(!capture.isOpened()){
         cout<<"fail to open "<<argv[2]<<endl;
         return -1 ;
     }
     Mat frame;
     Mat last_frame;
     Mat accumulative_frame ;
     int frame_cnt=0;
     while(1)
     {

         if(!capture.read(frame))
         {
             cout<<"读取视频失败"<<endl;
             break;
         }

         cv::cvtColor(frame, frame, CV_BGR2GRAY);
         cv::resize(frame, frame, cv::Size(800, 600), (0, 0), (0, 0), cv::INTER_LINEAR);
         cout<<"读取帧："<<frame_cnt<<endl;
         imshow("origin_frame",frame);

         if(frame_cnt>1){

        	 Mat diff_frame = abs(frame - last_frame);
        	 imshow("diff_frame",diff_frame);
        	 if(frame_cnt == 2){
        		 accumulative_frame = diff_frame ;
        	 }else{
        		 accumulative_frame = accumulative_frame + diff_frame ;
        	 }
        	 imshow("accumulative_frame",accumulative_frame);
         }
         last_frame = frame.clone();

         frame_cnt++;
         if(g_step){
        	 waitKey(0);
         }else{
             waitKey(1);
         }
     }
     capture.release();
     return 0;
 }

